package whf.framework.tools.generator.db;

import java.beans.PropertyDescriptor;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import whf.framework.entity.Entity;
import whf.framework.meta.service.ObjectService;
import whf.framework.meta.service.ObjectServiceImp;
import whf.framework.util.BeanUtils;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 * 
 */
public class Column {
	private String columnName;

	private boolean isPrimaryKey;

	private String dataType;

	private boolean isForeign;

	private boolean isUnique;

	private String referencedTable;

	private String referencedColumn;

	private String columnComment;

	private boolean nullable;

	private int characterMaxLength;

	private Table table;
	
	private String propertyType;

	private Column() {

	}

	public Column(Table table, String name, String dataType,
			String columnComment, boolean isnullable, int characterMaxLength) {
		this.columnName = name;
		this.dataType = dataType;
		this.table = table;
		this.nullable = isnullable;
		this.characterMaxLength = characterMaxLength;
		this.columnComment = columnComment;
	}

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public boolean isForeign() {
		return isForeign;
	}

	public void setForeign(boolean isForeign) {
		this.isForeign = isForeign;
	}

	public boolean isPrimaryKey() {
		return isPrimaryKey;
	}

	public void setPrimaryKey(boolean isPrimaryKey) {
		this.isPrimaryKey = isPrimaryKey;
	}

	public String getReferencedColumn() {
		return referencedColumn;
	}

	public void setReferencedColumn(String referencedColumn) {
		this.referencedColumn = referencedColumn;
	}

	public String getReferencedTable() {
		return referencedTable;
	}

	public void setReferencedTable(String referencedTable) {
		this.referencedTable = referencedTable;
	}

	public boolean isUnique() {
		return isUnique;
	}

	public void setUnique(boolean isUnique) {
		this.isUnique = isUnique;
	}

	public String getPropertyName() {
		String cn = this.columnName;
		if (StringUtils.equalsIgnoreCase("boolean", this.dataType)
				&& cn.toLowerCase().startsWith("is")) {
			cn = cn.substring(2);
		}
		String pn = Utils.getPropertyName(cn);
		if(pn.endsWith("Id") && this.columnName.toLowerCase().endsWith("_id")){
			pn = pn.substring(0, pn.length() - 2);
		}
		String spn = pn;
		if (pn.toLowerCase().startsWith(table.getBoClassName().toLowerCase())) {
			pn = pn.substring(table.getBoClassName().length());
			if(pn.length() == 0) pn = spn;
			pn = pn.substring(0, 1).toLowerCase() + pn.substring(1);
		}
		return pn.trim();
	}

	//如果是应用表外键，则从数据库中获取数据；
	public String getPropertyType() {
		String propertyName = this.getPropertyName();
		PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(Entity.class, propertyName);
		if(pd != null){
			return pd.getPropertyType().getName();
		}
		if(this.isForeign && !StringUtils.isEmpty(this.getReferencedTable()) && StringUtils.isEmpty(this.propertyType)){
			try{
				ObjectService service = ObjectServiceImp.getObjectService();
				whf.framework.meta.entity.Object referencedObj = service.findByTableName(this.getReferencedTable());
				if(referencedObj != null){
					this.propertyType = referencedObj.getBoClassName();
				}
			}catch(Exception e){
				//ignore
			}
		}
		if(StringUtils.isEmpty(this.propertyType)){
			this.propertyType = Utils.getMapType(this.dataType, Integer.MIN_VALUE);
		}
		return this.propertyType;
	}

	public String toString() {
		return ToStringBuilder.reflectionToString(this,
				ToStringStyle.MULTI_LINE_STYLE);
	}

	public String getColumnComment() {
		return columnComment;
	}

	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}

	public boolean isNullable() {
		return nullable;
	}

	public void setNullable(boolean nullable) {
		this.nullable = nullable;
	}

	public int getCharacterMaxLength() {
		return characterMaxLength;
	}

	public void setCharacterMaxLength(int characterMaxLength) {
		this.characterMaxLength = characterMaxLength;
	}

	public static void main(String[] args) throws Exception {
		Column col = new Column();
		col.setDataType("varchar");
		System.out.println(col.getPropertyType());
	}
}